package com.nbjtjc.safe.service.safe;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectPublicMapper;
import com.nbjtjc.safe.mapper.safe.*;
import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.safe.model.safe.SafeDangerInspectPlan;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTask;
import com.nbjtjc.safe.model.safe.SafeEnterprisePlan;
import com.nbjtjc.safe.util.MyShiroUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SafeEnterprisePlanServiceImpl {
    @Autowired
    private SafeEnterprisePlanMapper safeEnterprisePlanMapper;

    @Autowired
    private DangerInspectPublicMapper dangerInspectPublicMapper;
    @Autowired
    private SafeDangerInspectPlanMapper safeDangerInspectPlanMapper;

    @Autowired
    private SafeDangerInspectTaskServiceImpl safeDangerInspectTaskService;

    @Autowired
    private SafeDangerInspectTaskMapper safeDangerInspectTaskMapper;
    @Autowired
    private DangerInspectPublicMapper inspectPublicMapper;
    @Autowired
    private SafeDangerInspectTaskRecordMapper recordMapper;

    public void addBatch(SafeDangerInspectPlan safeDangerInspectPlan) {
        if (ObjectUtil.isNotNull(safeDangerInspectPlan)
                && CollectionUtil.isNotEmpty(safeDangerInspectPlan.getSafeEnterprisePlanList())) {
            safeDangerInspectPlan.getSafeEnterprisePlanList().forEach(safeEnterprisePlan -> {
                safeEnterprisePlan.setPlanId(safeDangerInspectPlan.getId());
            });
            safeEnterprisePlanMapper.insertList(safeDangerInspectPlan.getSafeEnterprisePlanList());
        }
    }


    public Result updateEnterprisePlan(List<SafeEnterprisePlan> safeEnterprisePlanList
            ,SafeDangerInspectPlan plan) {
        Result result = new Result();
        List<SafeEnterprisePlan> enterprisePlanList =
                safeEnterprisePlanMapper.findByPlanId(plan.getId());
        Set<Integer> addEnterpriseIds = new HashSet<>();
        Set<Integer> deleteEnterpriseIds = new HashSet<>();
        Set<Integer> originalEnterpriseIds = new HashSet<>();
        originalEnterpriseIds.addAll(enterprisePlanList.stream().
                map(SafeEnterprisePlan::getEnterpriseId).collect(Collectors.toSet()));
        addEnterpriseIds.addAll(safeEnterprisePlanList.stream()
                .map(SafeEnterprisePlan::getEnterpriseId).collect(Collectors.toSet()));
        deleteEnterpriseIds.addAll(originalEnterpriseIds);

        deleteEnterpriseIds.removeAll(addEnterpriseIds);
        addEnterpriseIds.removeAll(originalEnterpriseIds);
        enterprisePlanList.forEach(enterprisePlan->{
            if(deleteEnterpriseIds.contains(enterprisePlan.getEnterpriseId())){
                deleteEnterprisePlan(enterprisePlan);
            }
        });
        safeEnterprisePlanList.forEach(enterprisePlan->{
            if(addEnterpriseIds.contains(enterprisePlan.getEnterpriseId())){
                enterprisePlan.setPlanId(plan.getId());
                addEnterprisePlan(enterprisePlan);
            }
        });
        return result;
    }


    /**
     * 检查计划中添加企业，同时根据计划的检查表，生成该企业的检查任务和检查表
     *
     * @param safeEnterprisePlan
     */
    public Result addEnterprisePlan(SafeEnterprisePlan safeEnterprisePlan) {
        Result result = new Result();
        safeEnterprisePlanMapper.insert(safeEnterprisePlan);
        Example example = new Example(DangerInspectPublic.class);
        example.and().andEqualTo("inspectPlanId", safeEnterprisePlan.getPlanId());
        List<DangerInspectPublic> dangerInspectPublicList =
                dangerInspectPublicMapper.selectByExample(example);
        if (CollectionUtil.isNotEmpty(dangerInspectPublicList)) {
            SafeDangerInspectPlan safePlan =
                    safeDangerInspectPlanMapper.selectByPrimaryKey(safeEnterprisePlan.getPlanId());
            safeDangerInspectTaskService.generateTaskByEnterprise(dangerInspectPublicList, safePlan
                    , MyShiroUtils.getSessionUserId(), safeEnterprisePlan);
        }
        result.setData(safeEnterprisePlan);
        return result;
    }

    /**
     * 删除检查计划中的企业
     * 1、删除检查计划企业
     * 2、删除检查计划企业中对应的检查任务
     * 3、删除检查计划企业中的检查计划表
     *
     * @param safeEnterprisePlan
     */
    public void deleteEnterprisePlan(SafeEnterprisePlan safeEnterprisePlan) {
        safeEnterprisePlanMapper.deleteByPrimaryKey(safeEnterprisePlan.getId());
        Example taskExample = new Example(SafeDangerInspectTask.class);
        taskExample.and().andEqualTo("enterpriseId", safeEnterprisePlan.getEnterpriseId())
                .andEqualTo("inspectPlanId", safeEnterprisePlan.getPlanId());
        List<SafeDangerInspectTask> taskList =
                safeDangerInspectTaskMapper.selectByExample(taskExample);
        if (CollectionUtil.isNotEmpty(taskList)) {
            taskList.forEach(task -> {
                System.out.println("删除企业检查任务和检查计入和检查表");
                safeDangerInspectTaskMapper.delete(task);//删除企业检查任务
                recordMapper.deleteByTaskId(task.getId());//删除企业检查记录
                inspectPublicMapper.deleteByTaskId(task.getId());//删除企业检查表
            });
        }
    }
}
